//
//  StatusManager.m
//  Pennyworth Punch Clock
//
//  Created by Chris Karr on 8/6/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "StatusManager.h"
#import "AppDelegate.h"

#define MARGIN 10
#define PADDING 8

@implementation StatusManager

@synthesize state;

- (IBAction) toggleStatus:(id) sender;
{
	[NSApp activateIgnoringOtherApps:YES];
	[panel makeKeyAndOrderFront:sender];
}

- (void) windowDidResize:(NSNotification *) theNote
{
	NSView * contentView = [panel contentView];
	
	for (NSView * subview in [NSArray arrayWithArray:[contentView subviews]])
	{
		[subview removeFromSuperview];
	}
	
	NSSize size = [contentView frame].size;
	
	CGFloat textHeight = 14;

	CGFloat top = size.height - MARGIN - textHeight;
	
	CGFloat textWidth = (size.width - 8 - (2 * MARGIN)) / 2;
	
	NSFont * font = [NSFont labelFontOfSize:11];
	
	NSArray * keys = [[self.state allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
	
	for (NSString * key in keys)
	{
		if ((top - MARGIN) <= 0)
		{
			
		}
		else if ((top - MARGIN - PADDING - textHeight <= 0) && ![key isEqual:[keys lastObject]])
		{
			NSTextField * label = [[NSTextField alloc] init];
			[label setStringValue:@"More..."];
			[label setBordered:NO];
			[label setTextColor:[NSColor whiteColor]];
			[label setDrawsBackground:NO];
			[label setFont:font];
			[label setAlignment:NSCenterTextAlignment];
			[label setEditable:NO];
			[label setSelectable:NO];

			[contentView addSubview:label];
			[label setFrame:NSMakeRect(MARGIN, top, (textWidth * 2) + PADDING, textHeight)];
			[label release];
			
			top -= textHeight + PADDING;
		}
		else
		{
			NSTextField * keyLabel = [[NSTextField alloc] init];
			[keyLabel setStringValue:[NSString stringWithFormat:@"%@:", key]];
			[keyLabel setBordered:NO];
			[keyLabel setTextColor:[NSColor whiteColor]];
			[keyLabel setDrawsBackground:NO];
			[keyLabel setFont:font];
			[keyLabel setAlignment:NSRightTextAlignment];
			[[keyLabel cell] setLineBreakMode:NSLineBreakByTruncatingHead];
			[keyLabel setEditable:NO];
			[keyLabel setSelectable:NO];
			
			[contentView addSubview:keyLabel];
			[keyLabel setFrame:NSMakeRect(MARGIN, top, textWidth, textHeight)];
			[keyLabel release];
			
			NSTextField * valueLabel = [[NSTextField alloc] init];
			
			NSMutableString * stateValue = [NSMutableString string];
			
			for (NSString * value in [self.state valueForKey:key])
			{
				if ([stateValue length] > 0)
					[stateValue appendString:@"; "];
				
				[stateValue appendString:value];
			}
			
			[valueLabel setStringValue:stateValue];
			
			[valueLabel setBordered:NO];
			[valueLabel setTextColor:[NSColor whiteColor]];
			[valueLabel setDrawsBackground:NO];
			[valueLabel setFont:font];
			[valueLabel setAlignment:NSLeftTextAlignment];
			[[valueLabel cell] setLineBreakMode:NSLineBreakByTruncatingTail];
			[valueLabel setEditable:NO];
			[valueLabel setSelectable:NO];

			[contentView addSubview:valueLabel];
			[valueLabel setFrame:NSMakeRect((MARGIN + textWidth + 8), top, textWidth, textHeight)];
			[valueLabel release];
			
			top -= textHeight + PADDING;
		}
	}
	
	[contentView setNeedsDisplay:YES];
}

- (void) awakeFromNib
{
	[[NSDistributedNotificationCenter defaultCenter] addObserver:self 
														selector:@selector(update:) 
															name:PREDICTION_FETCHED 
														  object:nil];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(update:) 
												 name:PREDICTION_FETCHED 
											   object:nil];
}

- (void) update:(NSNotification *) theNote
{
	[[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateStatus:) userInfo:nil repeats:NO] retain];
}

- (void) updateStatus:(NSTimer *) theTimer
{
	[theTimer release];
	
	self.state = [((AppDelegate *) [NSApp delegate]) getActiveState];
	
	[self windowDidResize:nil];
}


@end
